package cn.ddiancan.xddcloud.psc.flow.entity;

import java.util.Collections;
import java.util.Date;
import java.util.Map;

import cn.ddiancan.xddcloud.psc.flow.context.XddcloudContext;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

/**
 * 流程实例实体类
 * @since 1.0
 */
@Getter
@Setter
@ToString
public class XddInstance extends FlowEntity {

    /**
     * 流程定义ID
     */
    protected Long processId;

    /**
     * 父流程实例ID
     */
    protected Long parentInstanceId;

    /**
     * 流程实例优先级
     */
    protected Integer priority;

    /**
     * 流程实例编号
     */
    protected String instanceNo;

    /**
     * 业务KEY（用于关联业务逻辑实现预留）
     *
     * 子流程情况，该字段用于存放父流程所在节点KEY

     */
    protected String businessKey;

    /**
     * 变量json
     */
    protected String variable;

    /**
     * 当前所在节点名称
     */
    protected String currentNodeName;

    /**
     * 当前所在节点key
     */
    protected String currentNodeKey;

    /**
     * 流程实例期望完成时间
     */
    protected Date expireTime;

    /**
     * 流程实例上一次更新人
     */
    protected Long lastUpdateBy;

    /**
     * 流程实例上一次更新时间
     */
    protected Date lastUpdateTime;

    public static XddInstance of(String businessKey) {
        XddInstance flwInstance = new XddInstance();
        flwInstance.setBusinessKey(businessKey);
        return flwInstance;
    }

    @SuppressWarnings({"all"})
    public Map<String, Object> variableToMap() {
        Map<String, Object> map = XddcloudContext.fromJson(this.variable, Map.class);
        if (map == null) {
            return Collections.emptyMap();
        }
        return map;
    }

    public void setMapVariable(Map<String, Object> args) {
        this.variable = XddcloudContext.toJson(args);
    }
}
